<template>
  <div class="page-container page-footer-btn">
    <van-cell-group>

      <van-row >
        <van-col span="18" >
          <FortenScan
            ref="scan"
            propPlaceholder="请扫描出库单号"
            propErrorMsg="请扫描出库单号"
            :propTypeArr="['sn','rfid']"
            @scanCall="handleScanOver"
          />
        </van-col>
        <van-col span="6" >
          <van-button size="small" type="primary" @click="reviewScn" :disabled="form.viewNum < 1 || form.flagFinished != 1">扫描盘点</van-button>
        </van-col>
      </van-row>
    </van-cell-group>

    <van-row class="check-row">
      <van-col span="6" style="color: red">实物多</van-col>
      <van-col span="6" style="color: #ff8917">实物少</van-col>
      <van-col span="6" style="color: green">实物正常</van-col>
      <van-col span="6" style="color: #1989fa">出库单</van-col>
    </van-row>
    <van-row class="check-row">
      <van-col span="6" style="color: red">{{form.surplus}}</van-col>
      <van-col span="6" style="color: #ff8917">{{form.lack}}</van-col>
      <van-col span="6" style="color: green">{{form.objects}}</van-col>
      <van-col span="6" style="color: #1989fa">{{form.viewNum}}</van-col>
    </van-row>

    <van-tabs v-model="active" >
      <van-tab title="实物多">
        <van-cell
            style="border-top:0.5px solid  #ccc"
            v-for="(item, index) in surplusRfids"
            :key="index"
            currency="">
            <template #title>

                <!--<van-row v-if="index == 0 " >
                  <van-col span="8">品名</van-col>
                  <van-col span="16">RFID</van-col>
                </van-row>
                <van-row>

                  <van-col span="8">未知</van-col>
                  <van-col span="16">{{item}}</van-col>
                </van-row>-->

              <van-row type="flex" >
                <van-col span="3">RFID:</van-col>
                <van-col span="21">{{item}}</van-col>
              </van-row>

            </template>
        </van-cell>
      </van-tab>

      <van-tab title="实物少">
        <van-cell
          style="border-top:0.5px solid  #ccc"
          v-for="(item, index) in lackObjects"
          :key="index"
          currency="">
          <template  #title  >
            <!--<van-row v-if="index == 0 ">
              <van-col span="8">品名</van-col>
              <van-col span="8">品规</van-col>
              <van-col span="8">RFID</van-col>
            </van-row>
            <van-row  >
              <van-col span="8">{{item.matrName}}</van-col>
              <van-col span="8">{{item.matrSpec}}</van-col>
              <van-col span="8">{{item.rfid}}</van-col>
            </van-row>-->
            <van-row  justify="start" type="flex">
              <van-col span="3">品名:</van-col>
              <van-col span="12">{{item.matrName}}</van-col>
              <van-col span="3">品规:</van-col>
              <van-col span="6">{{item.matrSpec}}</van-col>
            </van-row>
            <van-row type="flex" >
              <van-col span="3">RFID:</van-col>
              <van-col span="21">{{item.rfid}}</van-col>
            </van-row>
          </template>
        </van-cell>
      </van-tab>
      <van-tab title="实物正常">
        <van-cell
            style="border-top:0.5px solid  #ccc"
            v-for="(item, index) in objects"
            :key="index"
            currency="">
          <template  #title  >
            <!--<van-row v-if="index == 0 ">
              <van-col span="8">品名</van-col>
              <van-col span="8">品规</van-col>
              <van-col span="8">RFID</van-col>
            </van-row>
            <van-row  >
              <van-col span="8">{{item.matrName}}</van-col>
              <van-col span="8">{{item.matrSpec}}</van-col>
              <van-col span="8">{{item.rfid}}</van-col>
            </van-row>-->
            <van-row  justify="start" type="flex">
              <van-col span="3">品名:</van-col>
              <van-col span="12">{{item.matrName}}</van-col>
              <van-col span="3">品规:</van-col>
              <van-col span="6">{{item.matrSpec}}</van-col>
            </van-row>
            <van-row type="flex" >
              <van-col span="3">RFID:</van-col>
              <van-col span="21">{{item.rfid}}</van-col>
            </van-row>
          </template>
        </van-cell>
      </van-tab>
      <van-tab title="出库单明细">
        <van-cell
            style="border-top:0.5px solid  #ccc"
            v-for="(item, index) in outObjects"
            :key="index"
            currency="">
            <template  #title  >
                <!--<van-row v-if="index == 0 ">
                  <van-col span="8">品名:</van-col>
                  <van-col span="8">品规:</van-col>
                  <van-col span="8">RFID:</van-col>
                </van-row>-->
                <van-row  justify="start" type="flex">
                  <van-col span="3">品名:</van-col>
                  <van-col span="12">{{item.matrName}}</van-col>
                  <van-col span="3">品规:</van-col>
                  <van-col span="6">{{item.matrSpec}}</van-col>
                </van-row>
                <van-row type="flex" >
                  <van-col span="3">RFID:</van-col>
                  <van-col span="21">{{item.rfid}}</van-col>
                </van-row>
              </template>
         </van-cell>
      </van-tab>
    </van-tabs>

  </div>
</template>
<script>
/* eslint-disable*/
import barcodeParse from '@/util/barcodeParse.js'
import {Dialog} from "vant"

function subStringEnd(oldId) {
  let newStr = oldId
  for (let len = 0; len<oldId.length; len ++){
    if (newStr.endsWith("F")){
      newStr = newStr.substring(0,newStr.length-1)
    } else {
      return newStr
    }
  }
  return newStr
}

export default {
  data() {
    return {
      scan: '',
      form: {},
      warehouseList: [],
      active: '1',
      outObjects: [],//出货单实物
      objects: [],//扫描在单实物
      objectRfids: [],
      surplusRfids: [],
      lackObjects: [],
      lackRfids: []
    }
  },
  props: {
    propFlowKey: String
  },
  components: {
  },
  methods: {

    reviewScn(){
      window.scanConfirm = (rfidList) => {
        let rfids = []
        rfidList.forEach(rfid => {
          rfids.push(subStringEnd(rfid))
        })
        let scanObjects = [];
        for (let i = 0, len = this.outObjects.length; i < len; i++) {
          this.objectRfids.push(this.outObjects[i].rfid);
          for (let j = 0, length = rfidList.length; j < length; j++) {
            if (this.outObjects[i].rfid == rfids[j]) {
              scanObjects.push(this.outObjects[i]);
            }
          }
        }
        this.objects = scanObjects;
        this.$set(this.form, 'objects', scanObjects.length)
        this.surplusRfids = [];
        let surplusObjs = [];
        for (let j = 0, rfidListLen = rfids.length; j < rfidListLen; j++) {
          if (this.objectRfids.indexOf(rfids[j]) === -1) {
            surplusObjs.push(rfids[j])
          }
        }
        this.surplusRfids = surplusObjs;
        this.$set(this.form, 'surplus', surplusObjs.length);
        this.lackObjects = [];
        let lackObjs = [];
        for (let k = 0, objsLen = this.outObjects.length; k < objsLen; k++) {
          if (rfids.indexOf(this.outObjects[k].rfid) === -1) {
            lackObjs.push(this.outObjects[k])
          }
        }
        console.log(this.lackObjects);
        this.lackObjects = lackObjs;
        this.$set(this.form, 'lack', lackObjs.length)
        this.$set(this.form, 'flagFinished', 0)//扫描盘点按钮置灰
        //this.$refs.scan.focus()//焦点放到出库单输入框
      }
      //扫描枪扫描周边的RFID
      this.utilPrint.openRfidScan()
    },

    //扫描
    async handleScanOver(scanValue) {
      let wrapper = this.$newWrapper().eq('woKey', this.scan)
      let params = {
        isScan: 1,
        queryString: JSON.stringify(wrapper.queryConditions)
      }
      this.utils.loadShow()
      this.$ajax.get('/outCheck/list', {
        params
      }).then(resp => {
        if (resp.code == 0) {
          if (resp.msg.indexOf('成功') != -1) {
            this.$notify.success(resp.msg)
          } else {
            this.$notify.warning(resp.msg)
          }
          this.$set(this.form, 'viewNum', resp.data.length)
          this.$set(this.form, 'objects', 0)
          this.$set(this.form, 'surplus', 0)
          this.$set(this.form, 'lack', resp.data.length)
          this.outObjects = resp.data;
          this.lackObjects = this.outObjects;
          this.surplusRfids = [];
          this.objects = [];
          this.$set(this.form, 'flagFinished', 1)
          this.$refs.scan.focus()
          this.utils.loadHide()
        }
      })
    },

    //刷新列表
    reloadList() {
      try {
        let wrapper1 = this.$newWrapper().eq('wo_key', this.scan)
        this.$refs.wolistItem.setQueryWrapper(wrapper1)
      }catch (e) {
      }

    },

    //刷新选择
    reload(isByTop) {
      if (isByTop != true && this.form.flagFinished == 0) {
        this.$dialog.alert({
          title: '标题',
          message: '当前盘点任务尚未结束，请先结束!',
        }).then(() => {

        }).catch(() => {
        })
        return
      }
      this.form = {}
      this.form.stockNum = 0
      this.form.stockSurplus = 0
      this.form.stockUnknown = 0
      this.form.stockLoss = 0
      this.stocks = []
      this.form.warehouseId = this.utils.getCurrentWarehouse().warehouseId
      this.form.warehouseName = this.utils.getCurrentWarehouse().warehouseName
      this.reloadList()
      if (this.strings.isBlank(this.form.warehouseId)) {
        Dialog.alert({
          title: '系统提示',
          message: '请先到工作台设置当前仓库，再进行操作',
        })
          .then(() => {
            this.$store.commit('SET_WORKBENCH_ROUTE', { name: 'dashboard' })
          })
      }
    },
    getCheckStatus(checkStatus) {
      return this.checkStatus.filter(ob => ob.key ==checkStatus)[0]
    }
  },
  mounted() {
    this.reload()
  }
}
</script>

<style lang="scss" scoped>
  .van-col {
    text-align: center;
  }
  .popup {
    width: 60%;
    height: 100%;
  }
  .check-row {
    font-size: 18px;
    margin: 5px;
    .red {
      color: red
    }
    .green {
      color: green
    }
    .gray {
      color: gray
    }
  }
</style>
